import { BadRequestException, Injectable } from '@nestjs/common'
import { UserService } from 'src/user/user.service'
import { PrismaService } from 'src/common/prisma/prisma.service'
import { CreateAdminDto } from './dto/create-admin.dto'
import { JwtService } from 'src/jwt/jwt.service'
import * as bcrypt from 'bcryptjs'

@Injectable()
export class AdminService {
   constructor(
      private readonly userService: UserService,
      private readonly prismaService: PrismaService,
      private readonly jwtService: JwtService
   ) {}
   async findAllUser() {
      const allUser = await this.prismaService.user.findMany()
      return allUser
   }

   async findUserCount() {
      const userCountsByDay = await this.prismaService.user.groupBy({
         by: ['createDate'],
         _count: true
      })

      const count = await this.prismaService.user.count()

      return {
         userCountsByDay,
         count
      }
   }

   async getCommontCount() {
      const count = await this.prismaService.commont.count()
      return { count }
   }

   async getArticleByDay() {
      const articleCountsByDay = await this.prismaService.articel.groupBy({
         by: ['createDate'],
         _count: true
      })

      const count = await this.prismaService.articel.count()

      return { articleCountsByDay, count }
   }

   async findAllArticle() {
      const res = await this.prismaService.articel.findMany()
      return res
   }

   async login(loginDto: CreateAdminDto) {
      const res = await this.prismaService.admin.findFirst({
         where: {
            username: loginDto.username
         }
      })

      if (bcrypt.compareSync(loginDto.password, res.password)) return this.jwtService.getToken(res.id + '')
      throw new BadRequestException('密码错误')
   }

   async getCurrentUser(id: number) {
      return await this.prismaService.admin.findFirst({ where: { id } })
   }

   async deleteUser(id: number) {
      return await this.prismaService.user.delete({ where: { id } })
   }

   async findUserByUsername(username: string) {
      return await this.prismaService.user.findMany({ where: { username } })
   }

   async upUserStatus(id: number) {
      // 如果stause 为 1 则改为 0 为0改为1
      return await this.prismaService.user.update({
         where: { id },
         data: {
            status:
               (
                  await this.prismaService.user.findFirst({
                     where: { id }
                  })
               ).status === 1
                  ? 0
                  : 1
         }
      })
   }

   // 获取所有文章包括文章的类型信息和作者的信息
   async getArticleAll() {
      return await this.prismaService.articel.findMany({
         orderBy: {
            id: 'desc'
         },

         include: {
            category: true,
            user: true
         }
      })
   }

   // 更改文章状态
   async changeStatus(id: number, status: number) {
      return await this.prismaService.articel.update({
         where: {
            id
         },
         data: {
            status
         }
      })
   }

   // 获取所用评论
   async getAllCommont() {
      return await this.prismaService.commont.findMany({
         include: {
            user: true,
            articel: true
         }
      })
   }

   // 删除评论
   async deleteCommont(id: number) {
      return await this.prismaService.commont.delete({ where: { id } })
   }

   // 获取某个评论
   async getCommontById(id: number) {
      return await this.prismaService.commont.findFirst({ where: { id } })
   }

   // 根据用户名获取评论
   async getCommontByUserName(username: string) {
      return await this.prismaService.commont.findMany({
         where: {
            user: {
               username
            }
         },
         include: {
            user: true,
            articel: true
         }
      })
   }
}
